package cn.ziroom.contract.service;

import java.util.List;

import cn.common.util.BeanUtils;
import cn.ziroom.contract.data.PayData;
import cn.ziroom.contract.data.PayPlanData;
import cn.ziroom.contract.data.VoucherOrder;
import cn.ziroom.contract.domain.Bills;
import cn.ziroom.contract.domain.PayPlan;
import cn.ziroom.contract.domain.Referee;
import cn.ziroom.contract.mapper.BillsMapper;
import cn.ziroom.contract.mapper.PayPlanMapper;
import cn.ziroom.contract.mapper.RefereeMapper;

public class PayPlanService {

	private PayPlanMapper payPlanMapper;

	private BillsMapper billsMapper;
	
	private RefereeMapper refereeMapper;
	
	/**
	 * 合同收付款计划，根据资产合同ID全量更新收付款计划
	 * 
	 * @param contractData
	 * @return
	 */
	public String synchroPayPlan(PayData payData) {
		// 验证
		Integer sysContractId = payData.getSysContractId();
		String payType = payData.getPayType();
		if (sysContractId == null) {
			return "error_合同唯一标识(sysContractId)无值";
		}
		if(payType == null || payType.equals("")) {
			return "error_收付款计划(payType)无值";
		}
		List<PayPlanData> list = payData.getPayPlans();
		for (PayPlanData pay : list) {
			Integer id = pay.getSysContractId();
			String type = pay.getPayType();
			if (id == null) {
				return "error_合同唯一标识(sysContractId)无值";
			}
			if(type == null || type.equals("")) {
				return "error_收付款计划(payType)无值";
			}
		}
		
		// 根据资产合同ID删除收付款计划
		PayPlan payPlan = new PayPlan();
		payPlan.setSysContractId(sysContractId);
		payPlan.setPayType(payType);
		payPlanMapper.delete(payPlan);
		// 批量添加收付款计划
		for (PayPlanData pay : list) {
			PayPlan p = new PayPlan();
			BeanUtils.copyProperties(pay, p);
			payPlanMapper.insert(p);
		}
		
		// 是否含有推荐人
		if(payData.getSysCustId() != null && payData.getSysCustId() != 0) {
			// 根据收出房合同ID查询是否包含推荐人如果存在删除。
			Referee referee = new Referee();
			referee.setSysContractId(sysContractId);
			referee = refereeMapper.selectById(referee);
			if(referee != null && referee.getId() != null) {
				refereeMapper.deleteById(referee.getId());
			}
			
			// 保存推荐人信息
			referee = new Referee();
			referee.setSysContractId(sysContractId);
			referee.setSysCustId(payData.getSysCustId());
			referee.setCustName(payData.getCustName());
			referee.setTelphone(payData.getTelphone());
			referee.setCertNo(payData.getCertNo());
			refereeMapper.insert(referee);
		}
		
		
		return "success";
	}

	/**
	 * 收付款单据数据同步
	 * 
	 * @param voucherOrder
	 * @return
	 */
	public String synchroVoucherContract(VoucherOrder voucherOrder) {
		Integer sysPayPlanId = voucherOrder.getSysPayplanid();
		String payType = voucherOrder.getPayType();
		/*
		if(sysPayPlanId == null) {
			return "error_收付款计划ID(sysPayPlanId)不存在";
		}
		if(payType == null || payType.equals("")) {
			return "error_收付款单据类型(payType)不存在";
		}
		*/
		// 查找
		Bills bills = new Bills();
//		bills.setSysPayplanid(sysPayPlanId);
//		bills.setPayType(payType);
//		bills = billsMapper.selectById(bills);
//		if(bills != null) {
//			BeanUtils.copyProperties(voucherOrder, bills);
//			billsMapper.update(bills);
//		} else {
		bills = new Bills();
		BeanUtils.copyProperties(voucherOrder, bills);
		billsMapper.insert(bills);
//		}
		
		// 更改收付款计划状态
		PayPlan record = new PayPlan();
		record.setSysPayPlanId(sysPayPlanId);
		record.setStatus(voucherOrder.getPayPlanStatus());
		payPlanMapper.update(record);
		
		return "success";
	}
	
	/**
	 * 收付款单据数据同步
	 * 
	 * @param voucherOrder
	 * @return
	 */
	public String synchroVoucherContractAll(VoucherOrder voucherOrder) {
		Integer sysPayPlanId = voucherOrder.getSysPayplanid();
		String payType = voucherOrder.getPayType();
		// 查找
		Bills bills = new Bills();
		bills.setSysPayplanid(sysPayPlanId);
		bills.setPayType(payType);
		bills = billsMapper.selectById(bills);
		BeanUtils.copyProperties(voucherOrder, bills);
		billsMapper.insert(bills);
		return "success";
	}

	public void setPayPlanMapper(PayPlanMapper payPlanMapper) {
		this.payPlanMapper = payPlanMapper;
	}

	public void setBillsMapper(BillsMapper billsMapper) {
		this.billsMapper = billsMapper;
	}

	public void setRefereeMapper(RefereeMapper refereeMapper) {
		this.refereeMapper = refereeMapper;
	}
}
